package com.king.juc.syncmode;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 信箱类
 *      解耦结果产生线程与接收线程
 * @author king
 * @date 2020/11/5 18:54
 */
public class MailBoxes {

    private static Map<Integer, GuardedObjectV2> boxes = new ConcurrentHashMap<>();

    private static Integer id = 1;

    /**
     * 创建唯一性id
     * @return
     */
    private static synchronized int generateId(){
        return id++;
    }

    public static GuardedObjectV2 createGuardedObject(){
        GuardedObjectV2 go = new GuardedObjectV2(generateId());
        boxes.put(go.getId(), go);
        return go;
    }

    public static GuardedObjectV2 getGuardedObject(int id){
        return boxes.remove(id);
    }

    public static Set<Integer> getIds(){
        return boxes.keySet();
    }
}
